package com.xumingxun.aibox.domain.node;

import com.xumingxun.aibox.context.Context;
import com.xumingxun.aibox.domain.var.Var;
import com.xumingxun.aibox.enums.CustomTypeEnum;
import com.xumingxun.aibox.enums.NodeTypeEnum;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class CustomNode extends Node{

    public CustomTypeEnum customType;

    public String content;

    public Var var;

    public Var answerVar;

    public CustomNode(Context context) {
        super(context);

        init();
    }

    @Override
    public void init() {
        this.type = NodeTypeEnum.CUSTOM;
        this.customType=CustomTypeEnum.CUSTOM;
        this.export = false;
        this.nodeName = "Custom Node";
        answerVar=new Var(this.nodeName,"answer","内容");
        this.exportVars.add(answerVar);
    }

    @Override
    public Node execute() {
        Object obj=null;
        if(customType==CustomTypeEnum.CUSTOM) {
            obj=compileText(content);
        }else{
            obj=context.getRuntimeVarValue(var);
            if(obj==null){
                obj=context.getGlobalVarValue(var);
            }
        }
        context.setRuntimeVarValue(answerVar, obj);
        return next;
    }

    @Override
    public Object getResult() {
        if(this.export){
            return context.getRuntimeVarValue(answerVar);
        }
        return null;
    }

    public NodeTypeEnum getType() {
        return NodeTypeEnum.CUSTOM;
    }
}
